package com.sheeran.aichat.feign.bigmodel.fallback;

import com.sheeran.aichat.dto.bigmodel.BigModelResponseDTO;
import com.sheeran.aichat.dto.bigmodel.BigModelToolsResponseDTO;
import com.sheeran.aichat.dto.bigmodel.ChatCompletionRequestDTO;
import com.sheeran.aichat.dto.bigmodel.ChatToolsRequestDTO;
import com.sheeran.aichat.exception.BusinessException;
import com.sheeran.aichat.feign.bigmodel.BigModelClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * @author: sheeran
 * @create: 2025/02/10
 */
@Component
@Slf4j
public class BigModelFallback implements FallbackFactory<BigModelClient> {
    @Override
    public BigModelClient create(Throwable cause) {
        String message = cause == null ? "" : cause.getMessage();
        log.error(message, cause);

        return new BigModelClient() {

            @Override
            public BigModelResponseDTO completions(String token, ChatCompletionRequestDTO dto) {
                throw BusinessException.buildFromFeignCause(cause);
            }

            @Override
            public BigModelToolsResponseDTO tools(String token, ChatToolsRequestDTO dto) {
                throw BusinessException.buildFromFeignCause(cause);
            }
        };
    }
}
